package com.neusoft.neuiotms.dcvm.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.codahale.metrics.annotation.Timed;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.neuiotms.dcvm.dto.FileDTO;
import com.neusoft.neuiotms.dcvm.service.FileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("文件管理")
@RestController
@RequestMapping(value = "/api/w/files")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation("上传文件")
    @Timed
    @PostMapping
    public ResultDTO<FileDTO> upload(@RequestParam("file") MultipartFile file) {
        final FileDTO doc = this.fileService.save(file);
        return ResultDTO.success(doc);
    }

    @GetMapping("/{fsId}")
    public void download(@PathVariable String fsId, HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }
}
